﻿using System;
using System.Collections.Generic;
using JetBrains.Annotations;
using Missbot.Common;
using Missbot.Core.Cache;
using Missbot.Core.Scheduler;
using Missbot.Model.MsgCmds.Notes;

namespace Missbot.AIModule.Schedulers.DailyVtuber
{
    [UsedImplicitly]
    public class DailyVtuberScheduler : IScheduler
    {
        private const int SendHour = 9;

        protected override List<ScheduleMeta> ModelList { get; set; }

        public override string Owner => "iceai";

        private static VtuberPicSvc VtuberPicSvc => AutofacSvc.Resolve<VtuberPicSvc>();

        public override void Work()
        {
            var isAlreadySend = DailyVtuberDA.IsAlreadySend();
            var isOverSendHour = DateTime.Now.Hour >= SendHour;

            var model = new ScheduleMeta { IsImmediately = !isAlreadySend && isOverSendHour, Span = new FixHourSpan { FixHour = SendHour } };

            ModelList = new List<ScheduleMeta> { model };
            base.Work();
        }

        protected override void ScheduleDo(SchedulerTimer timer)
        {
            var pic = VtuberPicSvc.RandPic();
            MsgSender.PushMsg(new CreateNoteWithFileCmd
            {
                FileIDs = new[] { pic.FileID },
                Text = $"#今日のVtuber \n今日的 Vtuber是：\n***{pic.VtuberName}！***"
            });
            var rec = new DailyVtuberRecord { FileID = pic.FileID, VtuberName = pic.VtuberName };
            rec.Insert();
        }
    }
}